package com.cenwangkeji.resident.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.QuestionBean;
import com.cenwangkeji.resident.model.bean.User;
import com.cenwangkeji.resident.ui.activity.AddQuestionActivity;
import com.cenwangkeji.resident.ui.activity.MainActivity;
import com.cenwangkeji.resident.ui.activity.QuestionDetailActivity;
import com.cenwangkeji.resident.ui.adapter.QuestionAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.base.BaseFragment;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

/**
 * @松 on 2019/10/22 16:40
 * 寻医问药
 */
public class QuestionFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.rv)
    RecyclerView mRv;
    @BindView(R.id.iv)
    ImageView mIv;//添加问题
    Unbinder unbinder;
    private QuestionAdapter mAdapter;

    @Override
    public int setLayoutId() {
        return R.layout.fragment_question;
    }


    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        unbinder = ButterKnife.bind(this, rootView);
    }

    @Override
    protected void initData() {
        super.initData();
        initRecyclerView();

    }

    @Override
    public void onResume() {
        super.onResume();
        getQuestion();
    }

    private void initRecyclerView() {
        mRv.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new QuestionAdapter(null);
        mAdapter.setOnItemClickListener(this);
        mRv.setAdapter(mAdapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    //获取问题列表
    private void getQuestion() {
        showLoading();
        Observable.create(new ObservableOnSubscribe<QuestionBean>() {
            @Override
            public void subscribe(ObservableEmitter<QuestionBean> emitter) throws Exception {
                Map<Object, Object> map = new HashMap<>();
                map.put(Constant.token, User.getToken());
                map.put("mark", 3);
                map.put("userId", User.getUserId());
                JSONObject jsonObject = new JSONObject(map);
                String json = OkHttpUtils.post(jsonObject.toString(), Constant.question_list);
                MyLogger.i("寻医问药列表" + json);
                Gson gson = new Gson();
                emitter.onNext(gson.fromJson(json, QuestionBean.class));
            }
        }).compose(RxUtils.<QuestionBean>io_main()).subscribe(new ObserverOnNext<QuestionBean>() {
            @Override
            public void onNext(QuestionBean questionBean) {
                if (questionBean.getMsg().equals(Constant.token_expired)) {
                    BaseActivity activity = (MainActivity) getActivity();
                    activity.signIn();
                } else {
                    if (questionBean.getData() != null && questionBean.getData().size() > 0) {
                        mAdapter.setNewData(questionBean.getData());
                    } else {
                        View emptyView = getLayoutInflater().inflate(R.layout.view_empty, null);
                        mAdapter.setEmptyView(emptyView);

                    }
                }
                dismissLoading();
            }
        });
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        List<QuestionBean.DataBean> list = mAdapter.getData();
        Intent intent = new Intent(getActivity(), QuestionDetailActivity.class);
        intent.putExtra("date", list.get(position).getId());
        startActivity(intent);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        return rootView;
    }

    @OnClick(R.id.iv)
    public void onViewClicked() {
        startActivity(new Intent(getActivity(), AddQuestionActivity.class));
    }
}
